package com.jgy.threadput.service.impl;

import cn.hutool.json.XML;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jgy.threadput.config.TaskPoolConfig;
import com.jgy.threadput.entity.ThreadGte;
import com.jgy.threadput.service.ThreadGteService;
import com.jgy.threadput.mapper.ThreadGteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;

/**
* @author light
* @description 针对表【thread_gte】的数据库操作Service实现
* @createDate 2023-07-27 15:33:18
*/
@Service
@Transactional
public class ThreadGteServiceImpl extends ServiceImpl<ThreadGteMapper, ThreadGte>
    implements ThreadGteService{

    @Autowired
    private TaskPoolConfig taskPoolConfig;

    @Autowired
    private ThreadGteMapper gteMapper;

    @Override
    @Transactional(value = "transactionManager",rollbackFor = Exception.class)
    public Object insert() {
        //
        AtomicReference<Object> result = new AtomicReference<>();
        //造数据
        List<ThreadGte> list =newDataList();
        List<ThreadGte> threadGtes = newDataListTwo();
        List<List<ThreadGte>> saveList = batchList(list, 1000); //异常数据
        //List<List<ThreadGte>> list1 = batchList(threadGtes, 1000);//正常数据
        ThreadPoolTaskExecutor threadPoolTaskExecutor = taskPoolConfig.taskExecutor();
        saveList.forEach(subList -> {
            threadPoolTaskExecutor.execute(() -> gteMapper.insertList(subList));
        });
        //taskRun(result, saveList, threadPoolTaskExecutor);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    public void taskRun(AtomicReference<Object> result, List<List<ThreadGte>> list1, ThreadPoolTaskExecutor threadPoolTaskExecutor) {
        list1.forEach(subList -> {
            Future<?> submit = threadPoolTaskExecutor.submit(() -> this.saveBatch(subList));
            try {
                result.set(submit.get());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
        });
    }

    private List<ThreadGte> newDataList() {
        List<ThreadGte> list = new java.util.ArrayList<>();
        for (int i = 0; i < 20000; i++) {
            ThreadGte build = ThreadGte.builder().build();
            build.setId(UUID.randomUUID().toString());
            if (i==1){
                build.setId("1");
            }
            if (i==10200){
                build.setId("1");
            }
            build.setParam(String.valueOf(i));
            build.setNotes("第一批数据");
            list.add(build);
        }
        return list;
    }
    private List<ThreadGte> newDataListTwo() {
        List<ThreadGte> list = new java.util.ArrayList<>();
        for (int i = 0; i < 20000; i++) {
            ThreadGte build = ThreadGte.builder().build();
            build.setId(UUID.randomUUID().toString());
            build.setParam(String.valueOf(i));
            build.setNotes("第二批数据");
            list.add(build);
        }
        return list;
    }



    public static <T> List<List<T>> batchList(List<T> inputList, int batchSize) {
        List<List<T>> batches = new ArrayList<>();

        for (int i = 0; i < inputList.size(); i += batchSize) {
            int end = Math.min(i + batchSize, inputList.size());
            List<T> batch = inputList.subList(i, end);
            batches.add(batch);
        }
        return batches;
    }
}




